using System.Collections.Generic;
using JetBrains.Annotations;
using Unity.Plastic.Newtonsoft.Json;
using UnityEngine;

namespace HCore.Tests.Runtime.ShootingEditor2D.Scripts.System.GunSystem
{
    /// <summary>
    ///     枪械配置项类，用于存储单个枪械的基础属性信息。
    /// </summary>
    public class GunConfigItem
    {
        /// <summary>
        ///     初始化一个新的枪械配置项实例。
        /// </summary>
        /// <param name="name">枪械名称</param>
        /// <param name="bulletMaxCount">弹夹最大子弹数量</param>
        /// <param name="attack">每次攻击的伤害值</param>
        /// <param name="frequency">射击频率（每秒可射击次数）</param>
        /// <param name="shootDistance">有效射击距离</param>
        /// <param name="needBullet">是否需要消耗子弹</param>
        /// <param name="reloadSeconds">重新装填所需时间（秒）</param>
        /// <param name="shiftSeconds">切换到该枪械所需时间（秒）</param>
        /// <param name="description">枪械描述信息</param>
        public GunConfigItem(
            string name,
            int bulletMaxCount,
            float attack,
            float frequency,
            float shootDistance,
            bool needBullet,
            float reloadSeconds,
            float shiftSeconds,
            string description)
        {
            Name = name;
            BulletMaxCount = bulletMaxCount;
            Attack = attack;
            Frequency = frequency;
            ShootDistance = shootDistance;
            NeedBullet = needBullet;
            ReloadSeconds = reloadSeconds;
            ShiftSeconds = shiftSeconds;
            Description = description;
        }

        /// <summary>
        ///     名称
        /// </summary>
        public string Name { get; }

        /// <summary>
        ///     弹夹容量
        /// </summary>
        public int BulletMaxCount { get; }

        /// <summary>
        ///     伤害
        /// </summary>
        public float Attack { get; }

        /// <summary>
        ///     频率
        /// </summary>
        public float Frequency { get; }

        /// <summary>
        ///     射击距离
        /// </summary>
        public float ShootDistance { get; }

        /// <summary>
        ///     是否需要子弹
        /// </summary>
        public bool NeedBullet { get; }

        /// <summary>
        ///     填弹时间
        /// </summary>
        public float ReloadSeconds { get; }

        /// <summary>
        ///     切枪时间，捡枪可以忽略此时间，马上自动切枪
        /// </summary>
        public float ShiftSeconds { get; }

        /// <summary>
        ///     备注
        /// </summary>
        public string Description { get; }
    }

    /// <summary>
    ///     枪械配置管理类，提供根据名称获取枪械配置的功能。
    ///     内部预定义了几种常见枪械的配置数据。
    /// </summary>
    [IOC.Context.Stereotype.Component]
    public class GunConfig
    {
        // 初始化预定义的枪械配置字典
        private readonly Dictionary<string, GunConfigItem> mItems = new()
        {
            { "手枪", new GunConfigItem("手枪", 7, 1, 1, 0.5f, false, 3, 1, "默认的枪") },
            { "冲锋枪", new GunConfigItem("冲锋枪", 30, 1, 6, 0.34f, true, 3, 2, "弹药多") },
            { "步枪", new GunConfigItem("步枪", 50, 3, 3, 1f, true, 1, 3, "有后坐力") },
            { "狙击枪", new GunConfigItem("狙击枪", 12, 6, 1, 1f, true, 5, 3, "瞄准+超大后坐力") },
            { "火箭筒", new GunConfigItem("火箭筒", 1, 5, 1, 1f, true, 1, 4, "爆炸") },
            { "散弹枪", new GunConfigItem("散弹枪", 1, 1, 1, 0.5f, true, 3, 4, "一次性发射6~12枚子弹") }
        };

        /// <summary>
        ///     根据枪械名称获取对应的配置项。
        /// </summary>
        /// <param name="gunName">要查询的枪械名称</param>
        /// <returns>返回匹配的枪械配置项；如果未找到，则返回 null 并输出警告日志</returns>
        [CanBeNull]
        public GunConfigItem GetItemByName(string gunName)
        {
            if (mItems.TryGetValue(gunName, out var config)) return config;
            Debug.LogWarning($"没有找到枪：{gunName}; mItems: {JsonConvert.SerializeObject(mItems)}");
            return null;
        }
    }
}